// pages/home/home.js
const gd = getApp().globalData;
const net = require('../../../utils/net.js');
const util = require('../../../utils/util.js');
var posting = false;
var dialog=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo:"../../../assets/image/logo.png",
    cardheight:0,
    cardlist:[],
    classlist:[],
    itemindex:0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var sysinfo = gd.sysinfo;
    var w = sysinfo.windowWidth;
    this.setData({
      cardheight: (w - 100) * 29 * 2 / 46
    })
    dialog = this.selectComponent("#dialog");
    this.getcards();
  },
  preview(){
    console.log("preview");
    var len = this.data.cardlist.length;
    this.setData({
      itemindex: this.data.itemindex <= 0 ? len - 1 : this.data.itemindex-1
    })
  },
  next(){
    var len = this.data.cardlist.length;
    this.setData({
      itemindex: this.data.itemindex >= len ? 0 : this.data.itemindex + 1
    })
  },
  getcards(){
    if (posting){
      wx.stopPullDownRefresh();
      return;
    }
    posting = true;
    var that = this;
    wx.showLoading({})
    net.request({
      url: '/CardList',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id
      }, success(res) {
        var dt = res.data;
        if(dt.flag){
          for(var i=0;i<dt.data.length;++i){
            var time = dt.data[i]["start_date"].replace(/\-/g, ".") + "-" + dt.data[i]["end_date"].replace(/\-/g, ".");
            dt.data[i]["time"] = time;
            dt.data[i]["gym_logo"] = gd.gymlogo
          }
          that.setData({
            cardlist:dt.data
          })
        }else{
          dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {
        dialog.showDialog({
          msg: "服务器异常"
        })
      }, complete() {
        wx.hideLoading();
      }
    })
    posting = false;
    that.getclasslist();
  },
  getclasslist(){
    if (posting) return;
    posting = true;
    var that = this;
    wx.showLoading({})
    net.request({
      url: '/PrivateLessonList',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id,
        page: that.data.page
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          if(dt.data.length>0){
            var tmpclasslist = that.data.classlist;
            var page = that.data.page+1;
            for (var i = 0; i < dt.data.length; ++i) {
              var time = dt.data[i]["start_date"].replace(/\-/g, ".") + "-" + dt.data[i]["end_date"].replace(/\-/g, ".");
              dt.data[i]["time"] = time;
              tmpclasslist.push(dt.data[i]);
            }
            that.setData({
              page,
              classlist: tmpclasslist
            })
          }else{
            
          }
        } else {
          dialog.showDialog({
            msg: dt.msg
          })
        }
      }, fail(res) {

      }, complete() {
        posting = false;
        wx.hideLoading();
        that.scrollloading(false);
        wx.stopPullDownRefresh();
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  gocardlist(){
    util.Forward("../cardlist/cardlist")
  },
  goclasslist(){
    util.Forward("../classlist/classlist")
  },
  goclassdetail(e){
    var id = e.currentTarget.dataset.id;
    util.Forward("../classdetail/classdetail?id="+id);
  },
  gocarddetail(e){
    var id = e.currentTarget.dataset.id;
    util.Forward("../carddetail/carddetail?id=" + id);
  }, toscroll(e) {
    this.setData({
      scrolltmp: e.detail.scrollTop
    })
  }, tolower() {
    this.scrollloading(true);
    this.getclasslist();
  }, scrollloading(isshow) {
    var that = this;
    let top = 0;
    let delay = 0;
    if (isshow) {
      top = that.data.classlist.length * 60;
    } else {
      delay = 100;
      top = that.data.scrolltmp - 60;
    }
    setTimeout(function () {
      that.setData({
        scrollTop: top
      })
    }, delay);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      classlist: []
    })
    this.getcards();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})